package com.loooooo.pm2024.config;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.util.concurrent.Callable;

public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

    private final String headerName;

    private final Callable<String> headerValue;

    public HeaderRequestInterceptor(String headerName, Callable<String> headerValue) {
        this.headerName = headerName;
        this.headerValue = headerValue;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        try {
            request.getHeaders().set(headerName, headerValue.call());
        } catch (Exception exception) {

        }
        //request.getHeaders().set("Authorization", MyCommandLineRunner.token);
        return execution.execute(request, body);
    }
}

